package com.mall.controller;

import com.mall.common.AuthCheck;
import com.mall.common.Result;
import com.mall.entity.Category;
import com.mall.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类控制器
 * 
 * @author Mall Team
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    
    @Autowired
    private CategoryService categoryService;
    
    /**
     * 获取分类树
     */
    @GetMapping("/tree")
    public Result<List<Category>> getCategoryTree() {
        List<Category> categoryTree = categoryService.getEnabledCategoryTree();
        return Result.success(categoryTree);
    }
    
    /**
     * 获取所有分类树（包含禁用的）
     */
    @GetMapping("/all")
    @com.mall.common.AuthCheck
    public Result<List<Category>> getAllCategoryTree() {
        List<Category> categoryTree = categoryService.getCategoryTree();
        return Result.success(categoryTree);
    }
    
    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    @com.mall.common.AuthCheck
    public Result<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        return Result.success(category);
    }
    
    /**
     * 根据父ID获取子分类
     */
    @GetMapping("/children/{parentId}")
    @AuthCheck
    public Result<List<Category>> getCategoriesByParentId(@PathVariable Long parentId) {
        List<Category> categories = categoryService.getCategoriesByParentId(parentId);
        return Result.success(categories);
    }
    
    /**
     * 根据级别获取分类
     */
    @GetMapping("/level/{level}")
    @AuthCheck
    public Result<List<Category>> getCategoriesByLevel(@PathVariable Integer level) {
        List<Category> categories = categoryService.getCategoriesByLevel(level);
        return Result.success(categories);
    }
}